/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SignalFrame.java
 *
 * Created on 09.12.2011., 15.30.17
 */
package aor2.gui.cpu.signals;

import aor2.components.Signal;
import aor2.cpu.ClockCPU;
import javax.swing.JPanel;

/**
 * 
 * @author Moxi
 */
public class SignalFrame extends javax.swing.JFrame {

	/** Creates new form SignalFrame */
	public SignalFrame() {
		initComponents();
		clock = ClockCPU.getInstance();
		signal = new Signal(true);
		setCentralPane(new SignalPanel(signal, 10));
	}

	private Signal signal;
	private ClockCPU clock;

	public void setCentralPane(JPanel centralPanel) {
		if (centralPanel == null)
			return;
		drawPanel.setViewportView(centralPanel);
		pack();
		repaint();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		drawPanel = new javax.swing.JScrollPane();
		signalSet = new javax.swing.JPanel();
		jScrollPane2 = new javax.swing.JScrollPane();
		jScrollPane3 = new javax.swing.JScrollPane();
		active = new javax.swing.JToggleButton();
		jToggleButton1 = new javax.swing.JToggleButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		drawPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

		javax.swing.GroupLayout signalSetLayout = new javax.swing.GroupLayout(
				signalSet);
		signalSet.setLayout(signalSetLayout);
		signalSetLayout.setHorizontalGroup(signalSetLayout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 531,
				Short.MAX_VALUE));
		signalSetLayout.setVerticalGroup(signalSetLayout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 515,
				Short.MAX_VALUE));

		drawPanel.setViewportView(signalSet);

		jScrollPane2.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Kontrolni Signali"));

		jScrollPane3.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Signali Uslova"));

		active.setText("changeLevel");
		active.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				activeActionPerformed(evt);
			}
		});

		jToggleButton1.setText("clk++");
		jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jToggleButton1ActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addComponent(drawPanel,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										535,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(
														jScrollPane2,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														177, Short.MAX_VALUE)
												.addComponent(
														jScrollPane3,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														177, Short.MAX_VALUE)
												.addComponent(active)
												.addComponent(jToggleButton1))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(drawPanel, javax.swing.GroupLayout.DEFAULT_SIZE,
						519, Short.MAX_VALUE)
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addComponent(jScrollPane2,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										119,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(active)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jToggleButton1)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										86, Short.MAX_VALUE)
								.addComponent(jScrollPane3,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										254,
										javax.swing.GroupLayout.PREFERRED_SIZE)));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jToggleButton1ActionPerformed
		if (jToggleButton1.isEnabled()) {
			jToggleButton1.setEnabled(false);
			clock.nextClkPart();
			jToggleButton1.setEnabled(true);
		}

		pack();
		repaint();
	}// GEN-LAST:event_jToggleButton1ActionPerformed

	private void activeActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_activeActionPerformed
		if (active.isEnabled()) {
			active.setEnabled(false);
			signal.setActive(!signal.isActive());
			active.setEnabled(true);
		}
	}// GEN-LAST:event_activeActionPerformed

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		// <editor-fold defaultstate="collapsed"
		// desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase
		 * /tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(SignalFrame.class.getName())
					.log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(SignalFrame.class.getName())
					.log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(SignalFrame.class.getName())
					.log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(SignalFrame.class.getName())
					.log(java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				new SignalFrame().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JToggleButton active;
	private javax.swing.JScrollPane drawPanel;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JScrollPane jScrollPane3;
	private javax.swing.JToggleButton jToggleButton1;
	private javax.swing.JPanel signalSet;
	// End of variables declaration//GEN-END:variables
}
